/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2007>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using MyPhotoIndex.Utilities;

namespace MyPhotoIndex.Plugins.Share.Facebook.Fb_API
{
    public class PhotoAlbums
    {
        private const string FACEBOOK_HTTP_URI = "http://api.facebook.com/bestserver.php";

        Authentication m_authentication;

        public PhotoAlbums(Authentication authentication)
        {
            m_authentication = authentication;
        }

        public photos_getAlbums_responseAlbum[] GetAlbums()
        {
            HttpWebRequest request = HttpWebRequest.Create(FACEBOOK_HTTP_URI) as HttpWebRequest;
            RequestBuilder requestBuilder = new RequestBuilder(m_authentication.ApiKey, m_authentication.Secret);
            requestBuilder.InitializeRequest(request, "facebook.photos.getAlbums", m_authentication.SessionKey, true);

            String responseData = String.Empty;
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream);
                responseData = reader.ReadToEnd();
            }

            XmlSerializer serializer = new XmlSerializer(typeof(photos_getAlbums_response));
            photos_getAlbums_response getAlbumsResponse = (photos_getAlbums_response)serializer.Deserialize(new StringReader(responseData));

            return getAlbumsResponse.album;
        }

        public String CreateAlbum(string albumName)
        {
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(FACEBOOK_HTTP_URI) as HttpWebRequest;
                RequestBuilder requestBuilder = new RequestBuilder(m_authentication.ApiKey, m_authentication.Secret);

                requestBuilder.Add("name", albumName);
                requestBuilder.InitializeRequest(request, "facebook.photos.createAlbum", m_authentication.SessionKey, true);

                String responseData = String.Empty;
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);
                    responseData = reader.ReadToEnd();
                }

                XmlSerializer serializer = new XmlSerializer(typeof(photos_createAlbum_response));
                photos_createAlbum_response createAlbumResponse = (photos_createAlbum_response)serializer.Deserialize(new StringReader(responseData));

                return createAlbumResponse.aid;
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            return String.Empty;
        }
    }
}
